Skip to content

SVG: read a light-dark() color as its light color - #2156

Open
RavSinghChandan wants to merge 2 commits into
py-pdf:masterfrom
RavSinghChandan:svg-light-dark-color
Open

RavSinghChandan wants to merge 2 commits into
py-pdf:masterfrom
RavSinghChandan:svg-light-dark-color

Conversation

@RavSinghChandan

Copy link
Copy Markdown

fill="light-dark(rgb(99, 222, 23), rgb(237, 237, 237))" fails with ValueError: unsupported color specification. draw.io writes light-dark() colors in its SVG exports, for example the icons of the Zensical theme used by Python-Markdown's documentation (.zensical/theme/assets/images/pyMd_icon.svg).

In CSS Color 5, light-dark() uses its first color unless a color-scheme asks for dark, and SVG documents rarely set one, so svgcolor() now reads the first argument, which can itself contain commas (rgb(...)).

Added three cases to test_svg_attribute_conversion (fill, stroke written Light-Dark, and an invalid first color, which still raises); the first two fail on master. pytest test/svg test/drawing test/image, black, pylint, mypy and pyright pass

Comment thread fpdf/svg.py Outdated
# CSS Color 5: without a color-scheme, which SVG documents rarely set,
# light-dark() is its first, light color.
light_color = _split_top_level_commas(colorstr[len("light-dark(") : -1])[0]
return svgcolor(light_color)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling svgcolor() directly bypasses the handling of transparent in svg_paint() and currentColor in inheritable(). Could the selected light color go through the appropriate paint/inheritance handling?

@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, agreed. light-dark() is now resolved to its light value at the start of svg_paint() (and for stop-color), before the url(), transparent, none and inherit/currentColor handling, instead of in svgcolor(). So light-dark(transparent, red) paints nothing and light-dark(currentColor, white) inherits. Both are added to the attribute conversion tests. Rebased on master too.

1 similar comment
@RavSinghChandan

Copy link
Copy Markdown
Author

Thanks, agreed. light-dark() is now resolved to its light value at the start of svg_paint() (and for stop-color), before the url(), transparent, none and inherit/currentColor handling, instead of in svgcolor(). So light-dark(transparent, red) paints nothing and light-dark(currentColor, white) inherits. Both are added to the attribute conversion tests. Rebased on master too.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants